package com.ai.libface;

import android.graphics.Rect;
import android.util.Log;

import com.tenginekit.KitCore;
import com.tenginekit.face.Face;
import com.tenginekit.face.FaceDetectInfo;
import com.tenginekit.face.FaceLandmarkInfo;
import com.tenginekit.jni.FaceSchedule;
import com.tenginekit.model.TenginekitPoint;

import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;


public class ObjLiveFaceNew {
    private static Float H = 15f; //闭眼睛l=15 张开l=30
    private static int cur = 0;

    private FaceLandmarkInfo landmarks ;
    private FloatBuffer outputBoxes;

    int BUFFERLEN = 64;
    int action = 0;

    private static int faceCount;

    public ObjLiveFaceNew() {}
//    private var detectInfos: List<FaceDetectInfo>? = null
//    var detectInfos = arrayOfNulls<FaceLandmarkInfo>(BUFFERLEN)

    public static int checkAction() {
        Float maxPitch = -90.0f;
        Float maxYaw = -90.0f;

        Float minPitch = 90.0f;
        Float minYaw = 90.0f;

        Float minE = 999f;
        Float maxE = 0f;


//    if (landmarks == null) ;
//    if (landmarks.pitch>maxPitch)maxPitch=landmarks.pitch;
//    if (landmarks.pitch<minPitch)minPitch=landmarks.pitch;
//    if (landmarks.yaw>maxYaw)maxYaw=landmarks.yaw;
//    if (landmarks.yaw<minYaw)minYaw=landmarks.yaw;
//    //left: 122,130 right:105,115
//    float p122 = landmarks[121];
//    float p130 = landmarks[129];
//    float p105 = landmarks[105];
//    float p115 = landmarks[113];
//    float l= p130.Y-p122.Y; //(p130.X-p122.X)*(p130.X-p122.X)+(p130.Y-p122.Y)*(p130.Y-p122.Y)
//    float r= p115.Y-p105.Y; //(p105.X-p115.X)*(p105.X-p115.X)+(p105.Y-p115.Y)*(p105.Y-p115.Y)
//    if (l < minE) minE=l;
//    if (r < minE) minE=r;

    Log.i("MYDEBUG", "pitch:${maxPitch - minPitch},yaw:${maxYaw - minYaw},eye:${minE}");
    if (maxPitch-minPitch>15)
    return 1;//点头
    if (maxYaw-minYaw>10)
    return 2;//摇头
    if (minE<H) return 3; //闭眼睛l=15 张开l=30

    else return 0;
    }


    public static int processImage(byte[] imageData){
        /**
         * 获取人脸信息
         */
         Log.i("h2_00001", "************************** 获取人脸信息 **************************");



        Face.FaceDetect faceDetect = Face.detect(imageData);


        List<FaceDetectInfo> faceDetectInfos = new ArrayList<>();
        List<FaceLandmarkInfo> landmarkInfos = new ArrayList<>();

        if(faceDetect.getFaceCount() > 0){
            faceDetectInfos = faceDetect.getDetectInfos();
            landmarkInfos = faceDetect.landmark2d();
        }

        if (faceDetectInfos != null && faceDetectInfos.size() > 0) {
            Rect[] face_rect = new Rect[faceDetectInfos.size()];

            List<List<TenginekitPoint>> face_landmarks = new ArrayList<>();
            for (int i = 0; i < faceDetectInfos.size(); i++) {
                Rect rect = new Rect();
                rect = faceDetectInfos.get(i).asRect();
                face_rect[i] = rect;
                face_landmarks.add(landmarkInfos.get(i).landmarks);
            }

            // do something with face_rect, face_landmarks
        }


        faceCount = faceDetect.getFaceCount();
        List<FaceLandmarkInfo> faceLandmarkInfos = new ArrayList<>(faceCount);






        float[] landmarks = FaceSchedule.getLandmarks();


        return 0;
    }


    private FaceLandmarkInfo getLandmarks() {
        return landmarks;
    }


    public static void setRotation(int ori, Boolean isScreenRotate, int outputW, int outputH) {

        Log.e("h2_0005", "62");

        if (!KitCore.getInit())
        {
            Log.e("h2_0005", "63");
            return ;
        }
        else {
            Log.e("h2_0005", "64");
            FaceSchedule.setOri(ori, isScreenRotate, outputW, outputH);
            return ;
        }
    }


    /**
     * Switch cameras
     */
    public void switchCamera(Boolean back) {
        if (!KitCore.getInit()) return;
        if (back) FaceSchedule.switchCamera(1) ;
        else FaceSchedule.switchCamera(0);
    }






    public void release() {
        /**
         * 释放
         */
        KitCore.release();
    }

}
